﻿// -----------------------------------------------------------------------
// <copyright file="ConvertersExtensions.cs" company="Matthieu Klotz">
// Copyright (c) 2012, Matthieu Klotz
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name of Matthieu Klotz nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// </copyright>
// <summary>Contains the class that have all methods for converting a type from another.</summary>
// -----------------------------------------------------------------------

namespace AppFabric.Admin.Client.Core.Helpers
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using AppFabric.Admin.Client.Core.Configuration;

    /// <summary>
    /// Contains methods for converting models to or from other type.
    /// </summary>
    internal static class ConvertersExtensions
    {
        /// <summary>
        /// Converts an instance of <see cref="EnvironmentElement"/> to a new instance of <see cref="Model.Environment"/>.
        /// </summary>
        /// <param name="environment">The environment configuration element to convert.</param>
        /// <returns>A new instance of <see cref="Model.Environment"/>.</returns>
        internal static Model.Environment ToEnvironment(this EnvironmentElement environment)
        {
            Contract.Requires<ArgumentNullException>(environment != null);

            Model.Environment modelEnvironment = new Model.Environment
            {
                Name = environment.Name,
                Description = environment.Description,
                IsDefault = environment.IsDefault
            };

            ClusterCollection clusterCollection = environment.Clusters;
            if (clusterCollection != null && clusterCollection.Count > 0)
            {
                modelEnvironment.Childs = new ObservableCollection<Model.Node>();
                foreach (ClusterElement clusterElement in clusterCollection)
                {
                    if (clusterElement != null)
                    {
                        Model.Cluster clusterModel = clusterElement.ToCluster(modelEnvironment);
                        modelEnvironment.Childs.Add(clusterModel);
                    }
                }
            }

            return modelEnvironment;
        }

        /// <summary>
        /// Converts an instance of <see cref="Model.Environment"/> to a new instance of <see cref="EnvironmentElement"/>.
        /// </summary>
        /// <param name="environment">The environment to convert.</param>
        /// <returns>A new instance of <see cref="EnvironmentElement"/>.</returns>
        internal static EnvironmentElement ToEnvironmentElement(this Model.Environment environment)
        {
            Contract.Requires<ArgumentNullException>(environment != null);

            EnvironmentElement environmentElement = new EnvironmentElement
            {
                Name = environment.Name,
                Description = environment.Description
            };

            if (environment.Childs != null && environment.Childs.Count > 0)
            {
                environmentElement.Clusters = new ClusterCollection();
                foreach (Model.Cluster clusterModel in environment.Childs)
                {
                    ClusterElement clusterElement = clusterModel.ToClusterElement();
                    if (clusterElement != null)
                    {
                        environmentElement.Clusters.Add(clusterElement);
                    }
                }
            }

            return environmentElement;
        }

        /// <summary>
        /// Converts an instance of <see cref="ClusterElement"/> to a new instance of <see cref="Model.Cluster"/>.
        /// </summary>
        /// <param name="cluster">The cluster configuration element to convert.</param>
        /// <param name="parent">The parent environment of this cluster.</param>
        /// <returns>A new instance of <see cref="Model.Cluster"/>.</returns>
        internal static Model.Cluster ToCluster(this ClusterElement cluster, Model.Environment parent)
        {
            Contract.Requires<ArgumentNullException>(cluster != null);
            Contract.Requires<ArgumentNullException>(parent != null);

            Model.Cluster clusterModel = new Model.Cluster
            {
                Parent = parent,
                Name = cluster.Name,
                BaseServicesUri = new Uri(cluster.BaseServicesUri)
            };

            return clusterModel;
        }

        /// <summary>
        /// Converts an instance of <see cref="Model.Cluster"/> to a new instance of <see cref="ClusterElement"/>.
        /// </summary>
        /// <param name="cluster">The cluster to convert.</param>
        /// <returns>A new instance of <see cref="ClusterElement"/>.</returns>
        internal static ClusterElement ToClusterElement(this Model.Cluster cluster)
        {
            Contract.Requires<ArgumentNullException>(cluster != null);

            ClusterElement clusterElement = new ClusterElement
            {
                Name = cluster.Name,
                BaseServicesUri = cluster.BaseServicesUri.OriginalString
            };

            return clusterElement;
        }
    }
}
